﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebApi_For_Auth.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class PageWithControlController : ControllerBase
    {
        // GET: api/<PageWithControlController>
        [HttpGet]
        public List<DB.Models.PageWithControl> Get()
        {
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var data = db.PageWithControls
                    .Include(o => o.Page)
                    .Include(o=>o.RoleComparePageControls).ToList();
                return data;
            }
        }

        // GET api/<PageWithControlController>/5
        [HttpGet("{id}")]
        public DB.Models.PageWithControl Get(string id)
        {
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var data = db.PageWithControls
                    .Where(o => o.Id == id)
                    .Include(o => o.Page).SingleOrDefault();
                return data;
            }
        }

        // POST api/<PageWithControlController>
        [HttpPost]
        public void Post([FromBody] DB.Models.PageWithControl value)
        {
            if (value.Id == null)
            {
                value.Id = Guid.NewGuid().ToString();
            }
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                db.PageWithControls.Add(value);
                db.SaveChanges();
            }
        }

        // PUT api/<PageWithControlController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] DB.Models.PageWithControl value)
        {
            if (value.Id == null)
            {
                value.Id = Guid.NewGuid().ToString();
            }
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var pageWithControl = db.PageWithControls.Where(o => o.Id == value.Id).SingleOrDefault();
                if (pageWithControl != null)
                {
                    pageWithControl.Name = value.Name;
                    pageWithControl.Description = value.Description;
                    pageWithControl.UpdateTime = DateTime.Now;
                    pageWithControl.PageId = value.PageId;
                    db.SaveChanges();
                }
            }
        }

        // DELETE api/<PageWithControlController>/5
        [HttpDelete("{id}")]
        public void Delete(string id)
        {
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var pageWithControl = db.PageWithControls.Where(o => o.Id == id).SingleOrDefault();
                if (pageWithControl != null)
                {
                    db.PageWithControls.Remove(pageWithControl);
                    db.SaveChanges();
                }
            }
        }
    }
}
